// --------------------------------------------------------------------------------------------------------------------
// <copyright file="TermVector.cs" company="Arizona State University">
//   Copyright (C) 2011-2012 Sukru Tikves and Arizona State University
//   
//   Released under Microsoft Public License (Ms-PL)
//   See http://cipsdata.codeplex.com/license for up to date license 
//   informaiton.
// </copyright>
// <summary>
//   The term vector.
// </summary>
// --------------------------------------------------------------------------------------------------------------------

namespace Cips.DataMining
{
    using System;
    using System.Collections;
    using System.Collections.Generic;
    using System.Diagnostics.CodeAnalysis;
    using System.Linq;

    /// <summary>
    /// The term vector.
    /// </summary>
    /// <typeparam name="T">
    /// Keyword type.
    /// </typeparam>
    public abstract class TermVector<T> : Vector, IEnumerable<KeyValuePair<int, double>>
    {
        /// <summary>
        /// Gets the dictionary.
        /// </summary>
        public TermDictionary<T> Dictionary { get; private set; }

        /// <summary>
        /// Initializes a new instance of the <see cref="TermVector&lt;T&gt;"/> class.
        /// </summary>
        /// <param name="dictionary">The dictionary.</param>
        protected TermVector(TermDictionary<T> dictionary)
        {
            Dictionary = dictionary;
        }

        /// <summary>
        /// Gets or sets the values of the specified term.
        /// </summary>
        [SuppressMessage("StyleCop.CSharp.DocumentationRules", "SA1615:ElementReturnValueMustBeDocumented", Justification = "Reviewed. Suppression is OK here."),
        SuppressMessage("StyleCop.CSharp.DocumentationRules", "SA1611:ElementParametersMustBeDocumented", Justification = "Reviewed. Suppression is OK here.")]
        public abstract double this[T term] { get; set; }

        /// <summary>
        /// Gets or sets the values of the specified term.
        /// </summary>
        [SuppressMessage("StyleCop.CSharp.DocumentationRules", "SA1611:ElementParametersMustBeDocumented", Justification = "Reviewed. Suppression is OK here."),
        SuppressMessage("StyleCop.CSharp.DocumentationRules", "SA1615:ElementReturnValueMustBeDocumented", Justification = "Reviewed. Suppression is OK here.")]
        public abstract double this[int term] { get; set; }

        /// <summary>
        /// Returns a normalized copy of this vector.
        /// </summary>
        /// <returns>A normalized copy of this vector.</returns>
        public abstract TermVector<T> Normalize();

        /// <summary>
        /// Returns an enumerator that iterates through the collection.
        /// </summary>
        /// <returns>
        /// A <see cref="T:System.Collections.Generic.IEnumerator`1"></see> that can be used to iterate through the collection.
        /// </returns>
        public abstract IEnumerator<KeyValuePair<int, double>> GetEnumerator();

        /// <summary>
        /// Returns an enumerator that iterates through a collection.
        /// </summary>
        /// <returns>
        /// An <see cref="T:System.Collections.IEnumerator"/> object that can be used to iterate through the collection.
        /// </returns>
        IEnumerator IEnumerable.GetEnumerator()
        {
            return GetEnumerator();
        }

        /// <summary>
        /// Returns the KL-Divergence measure between this vector and the given second one.
        /// </summary>
        /// <param name="second">The second vector.</param>
        /// <returns>The KL-Divergence metric.</returns>
        public double KullbackLeiblerDivergence(TermVector<T> second)
        {
            if (Math.Abs(second.Data.Sum() - 0) < 0.0001 || Math.Abs(this.Data.Sum() - 0) < 0.0001)
                if (Math.Abs(second.Data.Sum() - 0) < 0.0001 && Math.Abs(this.Data.Sum() - 0) < 0.0001)
                    return 0;
                else
                    return double.PositiveInfinity;

            if (Math.Abs(this.Data.Sum() - 1) > 0.0001 || Math.Abs(second.Data.Sum() - 1) > 0.0001)
                throw new ArgumentException("KL-Divergence is only supported on vectors with sum = 1. Use Normalize() to fix this issue.");

            return this.Where(i => Math.Abs(second[i.Key] - 0) + Math.Abs(i.Value - 0) > 0.0001).Sum(i => i.Value * Math.Log(i.Value / second[i.Key]));
        }

        /// <summary>
        /// Calculates the cosine similarity of this vector and the given second one.
        /// </summary>
        /// <param name="second">The second vector.</param>
        /// <returns>Cosine similarity metric.</returns>
        public double CosineSimilarity(TermVector<T> second)
        {
            if (Math.Abs(second.Data.Sum() - 0) < 0.0001 || Math.Abs(this.Data.Sum() - 0) < 0.0001)
                if (Math.Abs(second.Data.Sum() - 0) < 0.0001 && Math.Abs(this.Data.Sum() - 0) < 0.0001)
                    return 1;
                else
                    return 0;

            return this.Sum(t => t.Value * second[t.Key]) / Data.Sum() / second.Data.Sum();
        }
    }
}